/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package model.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.dao.services.RoleDAOServices;
import model.entities.Role;

/**
 *
 * @author Ku81
 */
public class RoleDAO implements RoleDAOServices {
    private static RoleDAO roleDao;
    
    public static RoleDAO getInstance(){
        if(roleDao == null){
            roleDao = new RoleDAO();
        }
        return roleDao;
    }

    @Override
    public List<Role> getAllRoles() {
        List<Role> roleList = new ArrayList<Role>();
        try {
            Connection con = ConnectionFactory.getConnection();
            String sql = "SELECT * FROM tblRole";
            PreparedStatement ps = con.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while(rs.next()){
                Role r = new Role();
                r.setIdRole(rs.getInt("idRole"));
                r.setName(rs.getString("name"));
                r.setDescriptions(rs.getString("descriptions"));
                r.setIsActived(rs.getBoolean("isActived"));
                r.setUserList(UserDAO.getInstance().getByRoleId(rs.getInt("idRole")));
                roleList.add(r);
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(RoleDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(RoleDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return roleList;
    }

    @Override
    public Role getById(int id) {
        Connection con;
        Role role = null;
        try {
            con = ConnectionFactory.getConnection();
            String sql ="SELECT * FROM tblRole WHERE idRole=?";
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();
            while(rs.next()){
                role = new Role();
                role.setIdRole(rs.getInt("idRole"));
                role.setName(rs.getString("name"));
                role.setDescriptions(rs.getString("descriptions"));
                role.setIsActived(rs.getBoolean("isActived"));
//                role.setUserList(UserDAO.getInstance().getByRoleId(rs.getInt("idRole")));
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(RoleDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(RoleDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return role;
    }

    @Override
    public List<Role> getByName(String roleName) {
        Connection con;
        List<Role> roleList = new ArrayList<Role>();
        try {
            con = ConnectionFactory.getConnection();
            String sql ="SELECT * FROM tblRole WHERE name=?";
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, roleName);
            ResultSet rs = ps.executeQuery();
            while(rs.next()){
                Role role = new Role();
                role.setIdRole(rs.getInt("idRole"));
                role.setName(rs.getString("name"));
                role.setDescriptions("descriptions");
                role.setIsActived(rs.getBoolean("isActived"));
                role.setUserList(UserDAO.getInstance().getByRoleId(rs.getInt("idRole")));
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(RoleDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(RoleDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return roleList;
    }

    @Override
    public boolean create(Role role) {
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "INSERT INTO tblRole VALUES (?,?,?)";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, role.getName());
            pstmt.setString(2, role.getDescriptions());
            pstmt.setBoolean(3, role.isIsActived());
            return pstmt.executeUpdate() == 1;
            
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(RoleDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(RoleDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
    
    @Override
    public boolean update(Role role) {
        try {
            Connection con = ConnectionFactory.getConnection();
            String sql = "UPDATE tblRole SET name=?, descriptions=?, isActived=? WHERE idRole=?";
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, role.getName());
            ps.setString(2, role.getDescriptions());
            ps.setBoolean(3, role.isIsActived());
            ps.setInt(4, role.getIdRole());
            return ps.executeUpdate() == 1;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(RoleDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(RoleDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    @Override
    public boolean delete(int idRole) {
        try {
            Connection con = ConnectionFactory.getConnection();
            String sql = "DELETE tblRole WHERE idRole=?";
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setInt(1, idRole);
            return ps.executeUpdate() == 1;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(RoleDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(RoleDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;   
    }
    
    @Override
    public boolean enable(int idRole, boolean value) {
        try {
            Connection con = ConnectionFactory.getConnection();
            String sql = "UPDATE tblRole SET isActived=? WHERE isRole=?";
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setBoolean(1, value);
            ps.setInt(2, idRole);
            return ps.executeUpdate() == 1;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(RoleDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(RoleDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
}
